package com.zyz.rabbitextensionsdemo.listener;

import com.rabbitmq.client.Channel;
import com.zyz.rabbitextensionsdemo.constants.Constants;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class RetryListener {
//    @RabbitListener(queues = Constants.RETRY_QUEUE )
//    public void handMessage(Message message) {
//        long deliveryTag = message.getMessageProperties().getDeliveryTag();
//        System.out.printf("["+Constants.RETRY_QUEUE+"] 接收到消息：%s, deliveryTag： %d \n",
//                new String(message.getBody()), deliveryTag );
////        int num = 3/0;
//        System.out.println("业务处理完毕");
//    }

    @RabbitListener(queues = Constants.RETRY_QUEUE )
    public void handMessage(Message message, Channel channel) throws IOException {
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        System.out.printf("["+Constants.RETRY_QUEUE+"] 接收到消息：%s, deliveryTag： %d \n",
                new String(message.getBody()), deliveryTag );
        try {
//            int num = 3/0;
            System.out.println("业务处理完毕");
            channel.basicAck(deliveryTag, false);
        } catch (Exception e) {
            channel.basicNack(deliveryTag, false, true);
        }

    }
}



